export const style2StyleString = (style: object) => {
  const styleString = Object.entries(style)
    .map(item => {
      const lowerChar = item[0].match(/[A-Z]/g);
      if (lowerChar && lowerChar.length) {
        lowerChar.forEach(charItem => {
          item[0] = item[0].replace(charItem, `-${charItem.toLowerCase()}`);
        });
      }
      return item.join(':');
    })
    .join(';');
  return styleString;
};

export const elementOfPageRange = (x: number, y: number, ele: HTMLDivElement): { x: number; y: number } => {
  const width = ele.offsetWidth;
  const height = ele.offsetHeight;
  const { clientWidth } = document.documentElement;
  const { clientHeight } = document.documentElement;
  x = x < 0 ? 0 : x + width >= clientWidth ? clientWidth - width : x;
  y =
    y < document.documentElement.scrollTop ?
      document.documentElement.scrollTop :
      y + height >= clientHeight ?
        clientHeight - height :
        y;

  return { x, y };
};
